package com.example.opengldemo.vertex_shader_more.light_halo;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import android.view.MotionEvent;

import com.example.opengldemo.R;
import com.example.opengldemo.util.TextureUtil;

import static com.example.opengldemo.vertex_shader_more.light_halo.Constant.*;

class MySurfaceView extends GLSurfaceView
{	
	private SceneRenderer mRenderer;
	public Flare flare;
	
	public float lpx;
	public float lpy;
	float preX;
    float preY;
	public MySurfaceView(Context context){
		super(context);
        this.setEGLContextClientVersion(3);
        mRenderer = new SceneRenderer();
        setRenderer(mRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
	}
    
    @Override 
    public boolean onTouchEvent(MotionEvent e) 
    {
    	float x=e.getX();
    	float y=e.getY();
    	int action=e.getAction()&MotionEvent.ACTION_MASK; // 获取纯粹的事件信息
		switch(action)
		{
			case MotionEvent.ACTION_DOWN:
				preX=x;
				preY=y;
			break;	
			case MotionEvent.ACTION_MOVE:
				float dx=x-preX;
				float dy=y-preY;
				CameraUtil.changeDirection(dx*0.1f);
				CameraUtil.changeYj(dy*0.1f);
				preX=x;
				preY=y;
			break;
		}
		return true;
    }
    
	private class SceneRenderer implements Renderer
    {
		int[] textureIds=new int[3];
		DrawFlare df;
		TextureRect texRect;
		int[] textureIdA=new int[6];
		@Override
		public void onSurfaceCreated(GL10 gl, EGLConfig config)
		{			

            GLES30.glClearColor(0.0f,0.0f,0.0f,1.0f);    

            GLES30.glEnable(GLES30.GL_DEPTH_TEST);

            GLES30.glEnable(GLES30.GL_CULL_FACE);

            MatrixState.setInitStack();
          
			textureIds[0]= TextureUtil.getTextureId(MySurfaceView.this , R.drawable.flare1 , null , false);
			textureIds[1]= TextureUtil.getTextureId(MySurfaceView.this , R.drawable.flare2 , null , false);
			textureIds[2]= TextureUtil.getTextureId(MySurfaceView.this , R.drawable.flare3 , null , false);

            textureIdA[0]= TextureUtil.getTextureId(MySurfaceView.this ,R.raw.skycubemap_back , null , false);
            textureIdA[1]= TextureUtil.getTextureId(MySurfaceView.this ,R.raw.skycubemap_left , null , false);
            textureIdA[2]= TextureUtil.getTextureId(MySurfaceView.this , R.raw.skycubemap_right , null , false);
            textureIdA[3]= TextureUtil.getTextureId(MySurfaceView.this , R.raw.skycubemap_down , null , false);
            textureIdA[4]= TextureUtil.getTextureId(MySurfaceView.this , R.raw.skycubemap_up, null , false);
            textureIdA[5]= TextureUtil.getTextureId(MySurfaceView.this ,R.raw.skycubemap_front , null , false);

            
            flare=new Flare(textureIds);
			df=new DrawFlare(MySurfaceView.this);

            texRect=new TextureRect(MySurfaceView.this);  
		}

		@Override
		public void onSurfaceChanged(GL10 gl, int width, int height){

        	GLES30.glViewport(0, 0, width, height); 

        	RATIO = (float) width / height;
        	CameraUtil.init3DCamera();        
        	DIS_MAX=(int)Math.sqrt(RATIO*RATIO+1);        	
		}

		@Override
		public void onDrawFrame(GL10 gl)
		{

            GLES30.glClear(GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
            

            MatrixState.setProjectFrustum(-RATIO, RATIO, -1.0f, 1.0f, 2, 1000);
            CameraUtil.flush3DCamera();
            

            final float tzz=0.4f;

            MatrixState.pushMatrix();
            MatrixState.translate(0, 0, -UNIT_SIZE+tzz);
            texRect.drawSelf(textureIdA[0]);
            MatrixState.popMatrix();              

            MatrixState.pushMatrix();
            MatrixState.translate(0, 0, UNIT_SIZE-tzz);
            MatrixState.rotate(180, 0, 1, 0);
            texRect.drawSelf(textureIdA[5]);
            MatrixState.popMatrix(); 

            MatrixState.pushMatrix();
            MatrixState.translate(-UNIT_SIZE+tzz, 0, 0);
            MatrixState.rotate(90, 0, 1, 0);
            texRect.drawSelf(textureIdA[1]);
            MatrixState.popMatrix(); 

            MatrixState.pushMatrix();
            MatrixState.translate(UNIT_SIZE-tzz, 0, 0);
            MatrixState.rotate(-90, 0, 1, 0);
            texRect.drawSelf(textureIdA[2]);
            MatrixState.popMatrix();

            MatrixState.pushMatrix();
            MatrixState.translate(0, -UNIT_SIZE+tzz, 0);
            MatrixState.rotate(-90, 1, 0, 0);
            texRect.drawSelf(textureIdA[3]);
            MatrixState.popMatrix(); 

            MatrixState.pushMatrix();
            MatrixState.translate(0, UNIT_SIZE-tzz, 0);
            MatrixState.rotate(90, 1, 0, 0);
            texRect.drawSelf(textureIdA[4]);
            MatrixState.popMatrix();
            

        	float[] ls=CameraUtil.calLightScreen(RATIO);
			lpx=ls[0];
			lpy=ls[1];
			         
			if(lpx>RATIO||lpy>1)
			{
				return;
			}
			flare.update(lpx, lpy);
			

            MatrixState.setProjectOrtho(-RATIO, RATIO, -1.0f, 1.0f, 2, 1000);
            MatrixState.setCamera(0,0,0, 0,0,-1, 0,1,0);

            MatrixState.pushMatrix();
            
            GLES30.glEnable(GLES30.GL_BLEND);
            GLES30.glBlendFunc(GLES30.GL_SRC_COLOR, GLES30.GL_ONE);
            for(SingleFlare ss:flare.sFl)
            {
            	MatrixState.pushMatrix();
            	MatrixState.translate(ss.px, ss.py, -100+ss.distance);
            	MatrixState.scale(ss.bSize, ss.bSize, ss.bSize);
            	df.drawSelf(ss.texture,ss.color);
            	MatrixState.popMatrix();
            }
            GLES30.glDisable(GLES30.GL_BLEND);
            
            MatrixState.popMatrix();
     
		} 
    }
}
